package com.parkingspace.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.parkingspace.dao.PropertyMapper;
import com.parkingspace.entity.Property;
import com.parkingspace.service.IPropertyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 物业表 服务实现类
 * </p>
 *
 * @author Gapper
 * @since 2021-05-24
 */
@Service
public class PropertyServiceImpl extends ServiceImpl<PropertyMapper, Property> implements IPropertyService {
    @Resource
    private  PropertyMapper propertyMapper;

    List<Property> list=new ArrayList<Property>();

//    @Override
//    public List<Property> getPropertyByName(String property_name) {
//        System.out.println("impl进来了"+property_name);
//        QueryWrapper<Property> queryWrapper = new QueryWrapper<>();
//
//
//        queryWrapper.like("property_name",property_name);
//
//        List<Property> properties = propertyMapper.selectList(queryWrapper);
////        for (Property property : properties) {
////            System.out.println("impl查到的："+property);
////        }
//        return properties;
//    }



    @Override
    public IPage getPropertyList(String token,String propertyName, Page page) {
        System.out.println("impl进来了(全查)");
        List<Property> propertyList;
        IPage p=null;
        if (token==null){
            return null;
        }

        if (propertyName==null||propertyName==""){
            System.out.println("111111111111111111111111");
            QueryWrapper<Property> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("property_softdel",0);
//            propertyList = propertyMapper.selectList(null);
            p= propertyMapper.customPageQuery(page, queryWrapper);
        }else {

            System.out.println("222222222222222222222222222222");
            QueryWrapper<Property> queryWrapper = new QueryWrapper<>();
            queryWrapper.like("property_name", propertyName);
//            propertyList = propertyMapper.selectList(queryWrapper);
            p= propertyMapper.customPageQuery(page, queryWrapper);

        }
        return p;
    }

    @Override
    public boolean addProperty(Property property) {
        System.out.println("增加物业impl进来了"+property);

        if (property!=null){
            return propertyMapper.insert(property)==1?true:false;
        }
        return false;
    }

    @Override
    public boolean delPropertyById(Long propertyId) {
        System.out.println("删除物业impl进来了"+propertyId);
        if (propertyId!=null){
            int i = propertyMapper.deleteById(propertyId);
            return  i==1?true:false;
        }
        return false;
    }

    @Override
    public Property getPropertyById(Long propertyId) {
        System.out.println("根据id获取property进来了："+propertyId);
        Property property=null;
        if (propertyId!=null){
            property = propertyMapper.selectById(propertyId);
            return  property;
        }
        return property;
    }

    @Override
    public boolean updPropertyById(Property property) {
        System.out.println("根据物业id修改impl进来了："+property.getPropertyId());
        System.out.println("根据id修改物业信息进来了"+property.getPropertyName());
        System.out.println("根据id修改物业信息进来了"+property.getPropertyShare());
        System.out.println("根据id修改物业信息进来了"+property.getPropertyDetails());
        System.out.println("根据id修改物业信息进来了"+property.getPropertySoftdel());

        if (property!=null){
            Property p = new Property();
            p.setPropertyId(property.getPropertyId());
            p.setPropertyName(property.getPropertyName());
            p.setPropertyShare(p.getPropertyShare());
            p.setPropertyDetails(property.getPropertyDetails());
            p.setPropertySoftdel(0);
            return propertyMapper.updateById(p)==1?true:false;


        }
        return false;
    }

    @Override
    public boolean updPropertyShareById(Property property) {
        QueryWrapper<Property> queryWrapper = new QueryWrapper<>();
        return   propertyMapper.updateById(property)==1?true:false;


    }


}
